package com.hspedu.exception_.try_;

/**
 * @Author Stringzhua
 * @Date 2024/3/8 10:07
 * description:try-catch异常处理
 * ctrl+alt+t
 * 4.如果try代码块可能有多个异常,可以使用多个catch分别捕获不同的异常，相应处理
 * 要求子类异常写在前面,父类异常写在后面
 * catch (Exception e) {
 * System.out.println(e.getMessage());
 * }
 * catch (NullPointerException e) {
 * System.out.println("空指针异常=" + e.getMessage());
 * } catch (ArithmeticException e) {
 * System.out.println("算术异常=" + e.getMessage());
 * }
 * <p>
 * 报错:
 * Exception 'java.lang.NullPointerException' has already been caught
 * Exception 'java.lang.ArithmeticException' has already been caught
 */
public class TryCatchDetail02 {
    public static void main(String[] args) {
        try {
            Person person = new Person();
//            person = null;
            System.out.println(person.getName());//NullPointerException
            int n1 = 10;
            int n2 = 0;
            int res = n1 / n2;//ArithmeticException
        } catch (NullPointerException e) {
            System.out.println("空指针异常=" + e.getMessage());
        } catch (ArithmeticException e) {
            System.out.println("算术异常=" + e.getMessage());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
        }
    }
}

class Person {
    private String name = "jack";

    public String getName() {
        return name;
    }
}
